/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-30
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-14
 * @Description: 
 */
import 'package:hive/hive.dart';

part 'group_model.g.dart';

@HiveType(typeId: 3) // 0 代表存储的类型 ID，确保唯一
class Group extends HiveObject {
  @HiveField(0)
  String groupId;

  @HiveField(1)
  String groupName;

  @HiveField(2)
  String groupAvatar;

  @HiveField(3)
  int vertifyType;

  @HiveField(4)
  List<String> memberList;

  @HiveField(5)
  String notice;

  @HiveField(5)
  String? conversationId;

  Group({
    required this.groupId,
    required this.groupName,
    required this.groupAvatar,
    required this.vertifyType,
    required this.memberList,
    required this.notice,
    this.conversationId,
  });

  factory Group.fromJson(Map<String, dynamic> json) {
    final groupId = json['groupId'] ?? '';
    return Group(
      groupId: groupId,
      groupName: json['groupName'] ?? '',
      groupAvatar: json['groupAvatar'] ?? '',
      vertifyType: json['vertifyType'] ?? 0,
      notice: json['notice'] ?? '',
      memberList: _convertMembersToMemberList(json['members'], groupId),
    );
  }

  // 安全获取 List 方法
  static List<String> _convertMembersToMemberList(
      List<dynamic>? members, String groupId) {
    return members?.map<String>((m) => '$groupId${m['userId']}').toList() ?? [];
  }

  // 新增 copyWith 方法
  Group copyWith({
    String? groupName,
    String? groupAvatar,
    int? vertifyType,
    List<String>? memberList,
    String? notice,
    String? conversationId,
  }) {
    return Group(
      groupId: groupId,
      groupName: groupName ?? this.groupName,
      groupAvatar: groupAvatar ?? this.groupAvatar,
      vertifyType: vertifyType ?? this.vertifyType,
      memberList: memberList ?? List.from(this.memberList),
      notice: notice ?? this.notice,
      conversationId: conversationId ?? this.conversationId,
    );
  }
}

// 成员类型
@HiveType(typeId: 4)
class Member extends HiveObject {
  @HiveField(0)
  String memberId; // 自定义：由groupId + userId 组成

  @HiveField(1)
  String userId;

  @HiveField(2)
  String nickname;

  @HiveField(3)
  int role;

  @HiveField(4)
  String avatarUrl;

  @HiveField(5)
  int gender;

  Member({
    required this.memberId,
    required this.userId,
    required this.nickname,
    required this.role,
    required this.avatarUrl,
    required this.gender,
  });

  factory Member.fromJson(Map<String, dynamic> json, String groupId) {
    return Member(
      memberId: '$groupId${json['userId']}',
      userId: json['userId'] ?? '',
      nickname: (json['groupNickname'] ?? json['nickname']) ?? '',
      role: (json['role'] as int?) ?? 0,
      avatarUrl: json['avatarUrl'] ?? '',
      gender: (json['gender'] as int?) ?? 0,
    );
  }

  // 新增 copyWith 方法
  Member copyWith({
    String? nickname,
    int? role,
    String? avatarUrl,
    int? gender,
  }) {
    return Member(
      memberId: memberId,
      userId: userId,
      nickname: nickname ?? this.nickname,
      role: role ?? this.role,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      gender: gender ?? this.gender,
    );
  }
}
